package com.duoec.magichour.service.impl;

import com.duoec.magichour.dto.request.ArticleCommentSave;
import com.duoec.magichour.dto.response.ArticleCommentDto;
import com.duoec.magichour.dto.response.UserSimpleInfoDto;
import com.duoec.magichour.mapper.ArticleCommentMapper;
import com.duoec.magichour.pojo.ArticleComment;
import com.duoec.magichour.service.ArticleCommentService;
import com.duoec.magichour.service.ArticleService;
import com.duoec.magichour.service.UserService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ycoe on 17/6/27.
 */
@Service
public class ArticleCommentServiceImpl implements ArticleCommentService {
    @Autowired
    private ArticleCommentMapper articleCommentMapper;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;

    @Override
    public List<ArticleCommentDto> list(long articleId, int pageNo, int pageSize) {
        List<ArticleComment> comments = articleCommentMapper.getComments(articleId, (pageNo - 1) * pageSize, pageSize);
        if (comments == null || comments.isEmpty()) {
            return Lists.newArrayList();
        }
        List<ArticleCommentDto> articleCommentList = Lists.newArrayList();
        List<Long> userIds = Lists.newArrayList();
        comments.forEach(comment -> {
            ArticleCommentDto item = new ArticleCommentDto();
            BeanUtils.copyProperties(comment, item);

            if (!userIds.contains(comment.getUserId())) {
                userIds.add(comment.getUserId());
            }
            articleCommentList.add(item);
        });

        //获取用户信息
        if (!userIds.isEmpty()) {
            Map<Long, UserSimpleInfoDto> userMap = userService.getUserSimpleInfoMap(userIds);
            articleCommentList.forEach(comment -> {
                UserSimpleInfoDto user = userMap.get(comment.getUserId());
                if (user != null) {
                    comment.setUserAvatar(user.getAvatar());
                    comment.setUserName(user.getNickname());
                }
            });
        }

        return articleCommentList;
    }

    @Override
    public ArticleCommentDto save(long userId, long articleId, ArticleCommentSave articleCommentRequest) {
        ArticleComment ac = new ArticleComment();
        ac.setArticleId(articleId);
        ac.setComment(articleCommentRequest.getComment());
        ac.setCommentStatus((byte) 0);
        ac.setCreateTime(new Date());
        ac.setUserId(userId);

        //插入
        articleCommentMapper.insertSelective(ac);

        //往文章中添加回复数
        articleService.incArticleCommentCount(articleId);

        ArticleCommentDto dto = new ArticleCommentDto();
        BeanUtils.copyProperties(ac, dto);

        Map<Long, UserSimpleInfoDto> userMap = userService.getUserSimpleInfoMap(Lists.newArrayList(userId));
        if (!userMap.isEmpty()) {
            UserSimpleInfoDto userInfo = userMap.get(userId);
            dto.setUserAvatar(userInfo.getAvatar());
            dto.setUserName(userInfo.getNickname());
        }

        return dto;
    }

    @Override
    public boolean exists(long articleId, long commentId) {
        return articleCommentMapper.exists(articleId, commentId);
    }

    @Override
    public ArticleComment get(long id) {
        return articleCommentMapper.selectByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void delete(long articleId, long commentId) {
        articleCommentMapper.delete(commentId);

        //将文章里的评论数-1
        articleService.decArticleCommentCount(articleId);
    }
}
